from sqlalchemy import create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker
from config import get_config

settings = get_config()

engine = create_engine(
    'mysql+pymysql://%s:%s@%s:%d/%s?charset=utf8mb4' % (settings.DB_USER, settings.DB_PWD, settings.DB_HOST, settings.DB_PORT, settings.DB_NAME),
    echo=True
) 

SessionLocal = sessionmaker(bind=engine)

Base = declarative_base()

def get_db():
    current_db = SessionLocal()
    try:
        yield current_db
    finally:
        current_db.close()
